/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */
package com.ykht.proj.coupon.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ykht.proj.common.core.util.R;
import com.ykht.proj.common.security.util.SecurityUtils;
import com.ykht.proj.coupon.Enum.BarrageStatusEnum;
import com.ykht.proj.coupon.Enum.CommentStatusEnum;
import com.ykht.proj.coupon.entity.FxBarrage;
import com.ykht.proj.coupon.entity.FxCmsInfo;
import com.ykht.proj.coupon.entity.FxSensitiveWords;
import com.ykht.proj.coupon.mapper.FxBarrageMapper;
import com.ykht.proj.coupon.mapper.FxCmsInfoMapper;
import com.ykht.proj.coupon.service.FxBarrageService;
import com.ykht.proj.coupon.vo.FxBarrageVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 弹幕表
 *
 * @author proj code generator
 * @date 2021-07-07 16:20:00
 */
@Slf4j
@Service
@Transactional
@RequiredArgsConstructor
public class FxBarrageServiceImpl extends ServiceImpl<FxBarrageMapper, FxBarrage> implements FxBarrageService {

	private final FxBarrageMapper fxBarrageMapper;
	private final FxCmsInfoMapper fxCmsInfoMapper;

	@Override
	public R publish(FxBarrage fxBarrage) {

		if (StringUtils.isEmpty(fxBarrage.getContent())) return R.failed("弹幕为空");
		List<FxSensitiveWords> list = new FxSensitiveWords().selectList(Wrappers.lambdaQuery(FxSensitiveWords.class).select(FxSensitiveWords::getContent).eq(FxSensitiveWords::getStatus, 0));
		List<String> collect = list.stream().filter(o -> fxBarrage.getContent().replaceAll(" ", "").contains(o.getContent())).map(FxSensitiveWords::getContent).collect(Collectors.toList());

		boolean b = collect.size()>0;
		String s = collect.stream().collect(Collectors.joining(",", "触发敏感词:[", "]"));
//		StringBuilder builder = new StringBuilder().append("触发敏感词:").append(String.join(",", collect));
		FxCmsInfo cmsInfo = new FxCmsInfo().selectById(fxBarrage.getCmsId());
		fxBarrage.setStatus(b?2:1);
		fxBarrage.setReason(b?s:null);
		fxBarrage.setApprovalDate(b?LocalDateTime.now():null);
		fxBarrage.setCompanyId(cmsInfo.getCompanyId());
		fxBarrage.setBrandId(cmsInfo.getBrandId());
		fxBarrage.setType(0);
		fxBarrage.setPublishId(SecurityUtils.getPersonId());
		fxBarrage.setPublishName(SecurityUtils.getPerson().getNickName());
		fxBarrage.setCreateTime(LocalDateTime.now());
		fxBarrage.setCreateUser(SecurityUtils.getPersonId());
		fxBarrage.insert();
		return b?R.failed("存在敏感词汇"):R.ok();
	}

	/**
	 * 弹幕表 分页查询
	 *
	 * @param page        分页
	 * @param fxBarrageVo 条件
	 * @return
	 */
	@Override
	public IPage<FxBarrageVo> getBarrageListPage(Page page, FxBarrageVo fxBarrageVo) {
		return fxBarrageMapper.getBarrageListPage(page, fxBarrageVo);
	}

	/**
	 * pc新增弹幕表并同时给视频添加弹幕数量
	 *
	 * @param fxBarrage 弹幕表
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveFxBarrage(FxBarrage fxBarrage) {
		this.save(fxBarrage);
		fxCmsInfoMapper.updateStatisticsByCmsId(fxBarrage.getCmsId(),0, 0, 0, 0, 1);
	}

	/**
	 * pc批量新增弹幕表并同时给视频添加弹幕数量
	 *
	 * @param fxBarrageVo 弹幕表集合
	 * @return R
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveBatchFxBarrage(FxBarrageVo fxBarrageVo) {
		fxBarrageVo.setType(1);
		fxBarrageVo.setStatus(BarrageStatusEnum.AT_WORK.getValue());
		fxBarrageVo.setCreateUser(SecurityUtils.getUserId());
		fxBarrageVo.setCreateTime(LocalDateTime.now());
		fxBarrageVo.setApprovalDate(LocalDateTime.now());
		fxBarrageVo.setPublishId(SecurityUtils.getUserId());
		fxBarrageVo.setPublishName(SecurityUtils.getUser().getUsername());
		List<FxBarrage> barrageList = new ArrayList<>();
		fxBarrageVo.getContentList().stream().forEach(itme ->{
			FxBarrage fxBarrage = BeanUtil.toBean(fxBarrageVo, FxBarrage.class);
			fxBarrage.setContent(itme);
			barrageList.add(fxBarrage);
		});
		this.saveBatch(barrageList);
		fxCmsInfoMapper.updateStatisticsByCmsId(barrageList.get(0).getCmsId(),0, 0, 0, 0, barrageList.size());
	}

	/**
	 * pc 审核弹幕
	 *
	 * @param fxBarrageVo 弹幕表
	 * @return R
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateFxBarrage(FxBarrageVo fxBarrageVo) {
		this.updateById(BeanUtil.toBean(fxBarrageVo, FxBarrage.class));
		if (fxBarrageVo.getStatus().equals(BarrageStatusEnum.AT_WORK.getValue())){
			fxCmsInfoMapper.updateStatisticsByCmsId(fxBarrageVo.getCmsId(),0, 0, 0, 0, 1);
		} else if (fxBarrageVo.getStatus().equals(BarrageStatusEnum.BE_OVERDUE.getValue())
				&& fxBarrageVo.getOriginalStatus().equals(BarrageStatusEnum.AT_WORK.getValue())){
			fxCmsInfoMapper.updateStatisticsByCmsId(fxBarrageVo.getCmsId(),0, 0, 0, 0,-1);
		}
	}
}
